{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fd228efd-1096-4c13-93f7-43ab0b0d15bb",
   "metadata": {},
   "source": [
    "# 气动吸盘驱动 demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56cd1f28-6a29-43c3-a13e-22bc7a9487fd",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2c373107-5667-40ed-8edd-13a68611861b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from robodyno import Robot\n",
    "from robodyno.nodes import NanoBoardFactory\n",
    "robot = Robot()\n",
    "device_id = 0x21\n",
    "nano_board = robot.add_device('end_effector', NanoBoardFactory(), device_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "965c8d03-7dfa-44c5-817b-d16eafe5c835",
   "metadata": {},
   "source": [
    "## 吸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ae33a0ca-36aa-49ec-8a15-0bac88b8b26b",
   "metadata": {},
   "outputs": [],
   "source": [
    "nano_board.set_pwm(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa04147b-3fa8-4bd7-ae3d-75d5891e74c2",
   "metadata": {},
   "source": [
    "## 松"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f0727914-5b6e-47df-958f-ba08b7de87b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "nano_board.set_pwm(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52891a35-154d-4b6d-9930-2bf52d8272c2",
   "metadata": {},
   "source": [
    "## 交互"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9c45e463-a143-43cc-845a-514284659c79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8b2700d6661a453ca5c10b7126f54438",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "ToggleButton(value=False, button_style='danger', description='已关闭')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipywidgets as widgets\n",
    "button = widgets.ToggleButton(\n",
    "    description='已关闭',\n",
    "    button_style='danger',\n",
    ")\n",
    "def on_button_clicked(change):\n",
    "    if change.new == False:\n",
    "        button.description = '已关闭'\n",
    "        button.button_style = 'danger'\n",
    "        nano_board.set_pwm(0)\n",
    "    else:\n",
    "        button.description = '已开启'\n",
    "        button.button_style = 'success'\n",
    "        nano_board.set_pwm(1)\n",
    "button.observe(on_button_clicked, 'value')\n",
    "button"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a78a9bf-eaea-4707-9b15-dc1b6bfd8732",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
